//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "ShipmentReportDlg.h"
#include "FormUtilities.h"
#include "ShipmentsReport.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmShipmentReportDlg::TfrmShipmentReportDlg(TComponent* Owner, TDatabase * dbMain)
        : TForm( Owner)
{
   Word year;
   Word month;
   Word day;

   TFormUtilities::LocateForm(( TForm*)Owner, this);
   this->dbMain = dbMain;

   FillMonthCombo();
   providerBridge = new TProviderBridge(dbMain);
   cmbProviders->Sorted = true;
   FillProvidersCombo();
   DecodeDate( Now(), year, month, day);
   edtYear->Text = IntToStr( year);
   cmbMonth->ItemIndex = month - 1; 
}

void TfrmShipmentReportDlg :: FillMonthCombo()
{
   AnsiString month;
   int        i;

   for ( i = 1; i <= 12; i++)
   {
      DateTimeToString( month, "mmmm", EncodeDate( 2004, i, 1));
      cmbMonth->Items->Add( month);
   }//for
}

void TfrmShipmentReportDlg :: FillProvidersCombo()
{
   providerBridge->GetProviders( cmbProviders->Items);
   if (cmbProviders->Items->Count > 0)
        cmbProviders->ItemIndex = 0;     
}


void __fastcall TfrmShipmentReportDlg::btnCloseClick(TObject *Sender)
{
   Close();
}
//---------------------------------------------------------------------------

void __fastcall TfrmShipmentReportDlg::btnProduceClick(TObject *Sender)
{
   TfrmShipmentsReport * frmShipmentsReport;

   frmShipmentsReport = new TfrmShipmentsReport( this, dbMain);

   try
   {
      __int64 providerID;
      if ( cmbProviders->Items->Count == 0)
         providerID = -1;
      else
         providerID = ((TProvider*)cmbProviders->Items->Objects[ cmbProviders->ItemIndex])->id;

      frmShipmentsReport->Preview( StrToInt( edtYear->Text), cmbMonth->ItemIndex + 1, providerID);
   }
   __finally
   {
      delete frmShipmentsReport;
   }//try-finally
}
//---------------------------------------------------------------------------

void __fastcall TfrmShipmentReportDlg::FormDestroy(TObject *Sender)
{
   delete providerBridge;        
}
//---------------------------------------------------------------------------

